// miniprogram/pages/subPages/gonghuodan/gonghuodan.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2020-01-01',
    goods:[ ],
    marketName:"",
    index:0,
    addImagesList:[]
  },
// 日期选择
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 卖场选择
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  // 确定按钮
  ascertain:function(){
    const marketId = wx.getStorageSync("marketInfo")._id
    const marketName = wx.getStorageSync("marketInfo").name
    const totalPrice = wx.getStorageSync("totalPrice")
    console.log(totalPrice)
    // 供货时间
    var date=this.data.date
    // 供货商品
    var goods=this.data.goods
    console.log(goods)
    var newGoods=goods.map((item,index,arr)=>{
      return { "id": item._id,
               "name":item.name,
               "num":item.stepCount,
               "unitprice":item.price} 
    })
    console.log(newGoods)
    // 供货图片
    var supplypics= wx.getStorageSync('imgLists');
    //生成供货单

    var sendData={
      createDate: date,
      marketId: marketId,
      marketName: marketName,
      products: newGoods,
      status:0 ,
      supplyDate: date,
      supplypics:supplypics,
      total:totalPrice
    }
    console.log("构成的记过",sendData)
    wx.cloud.callFunction({
      name:"addSupply",
      data:{
        newData:{
          createDate: date,
          marketId: marketId,
          marketName: marketName,
          products: newGoods,
          status:0 ,
          supplyDate: date,
          supplypics:supplypics,
          total:totalPrice
        }
      },
      success:result=>{
        console.log(result)
      }
    })


    // 将总价格重置
    wx.setStorageSync("totalPrice", 0)
    // 将在内存总的产品重置
    wx.setStorageSync("saveProductList", [])
    // 将在内存中的卖场信息重置
    wx.setStorageSync("marketInfo",{})
    wx.switchTab({
      url: '../../index/index',
    })
  },

  // 修改
  modify:function(){
    // 根据商品Id
    // wx.navigateTo({
    //   url: '../gonghuo/gonghuo',
    // })
    wx.navigateBack({
      delta:2
    })
  },

  // 点击添加图片
  addImages:function(){
    var that=this
    wx.chooseImage({
      count:5,//限制上传的数量
      success:res=> {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        // 判断是否是多张图片
        if(tempFilePaths.length>5){
          wx.showToast({
            title: '最多只能上传5张',
            icon:"none",
            duration:2000
          })
          return false;
        }else{
          for(var i=0;i<tempFilePaths.length;i++){
            that.uploadImage(tempFilePaths[0])
          }
        }
       wx.hideLoading({
         success: (res) => {
           console.log("上传操作结束")
         },
       })
      },
    })
  },
  // 上传图片
  uploadImage(fileURL) {
    wx.showLoading({
      title: '上传中',
    })
    wx.cloud.uploadFile({
      cloudPath:new Date().getTime()+'.png', // 上传至云端的路径
      filePath: fileURL, // 小程序临时文件路径
      success: res => {
        // 返回文件 ID
        console.log("上传成功",res)
        //获取文件路径
        this.setData({
          addImagesList:[...this.data.addImagesList,res.fileID]
        })
        console.log("结果")
        console.log("上传的结果",this.data.addImagesList)
       var imgsList= wx.getStorageSync('imgLists')||[]
        wx.setStorageSync('imgLists', [...imgsList,...this.data.addImagesList])
      },
      fail: console.error
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var goodsList=wx.getStorageSync("saveProductList")||[]
// cloud://cloudwx-dm78q.636c-cloudwx-dm78q-1300838257/1603546162802.png
   
    wx.getStorage({
      key: 'marketInfo',
      success:res=>{
        console.log(res)
        this.setData({
          marketName:res.data.name
        })
      }
    })
    this.setData({
      goods:goodsList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var imgsList= wx.getStorageSync('imgLists')||[]
   console.log("图片列表",imgsList)
   this.setData({
    addImagesList:imgsList
   })
   var date= this.getDate();
   this.setData({
     date:date
   })
  },
  // 获取当前日期函数
  getDate:function(){
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1
    var day = date.getDate()
    if(month>9){
      
    }else{
      month="0"+month;
    }
    if(day>9){

    }else{
      day="0"+day
    }
    return year+'-'+month+'-'+day
  },

})